var e = function(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}(require("../../../api/api"));

getApp();

Page({
    data: {
        codeValidation: !1,
        codeRequired: !1,
        gift_card_code: null,
        errorMsg: null,
        successMsg: null,
        voucherNeedPurchase: !1
    },
    onLoad: function() {},
    bindKeyInput: function(e) {
        this.setData({
            gift_card_code: e.detail.value
        });
    },
    redeemCode: function() {
        var a = this;
        if (!a.data.gift_card_code) return a.setData({
            codeRequired: !0
        }), !1;
        var r = a.data.gift_card_code.toUpperCase().replace(/[-\s]/g, "");
        if (r && 16 != r.length) return a.setData({
            codeValidation: !0
        }), !1;
        var d = {
            code: r
        };
        e.default.redeemGiftCard({
            method: "POST",
            data: d,
            success: function(e) {
                if (e && e.data.data) a.setData({
                    success: e.data.data
                }); else if (e && e.data.error) {
                    var r = e.data.error.type, d = {};
                    if ("voucherNeedPurchase" !== r) {
                        switch (r) {
                          case "VoucherHasNotStarted":
                            d.code = "此促销活动还没有开始";
                            break;

                          case "VoucherHasExpired":
                            d.code = "该代码已经失效";
                            break;

                          case "VoucherHasBeenRedeemed":
                            d.code = "该代码已被兑换";
                            break;

                          case "VoucherDesignatedUser":
                            d.code = "该代码无效";
                            break;

                          case "VoucherNotValid":
                          case "VoucherNotFound":
                            d.code = "该代码无效";
                            break;

                          case "VoucherHasRedeemedOne":
                            d.code = "您已经在相同的促销活动兑换过该抵用券";
                        }
                        a.setData({
                            errorMsg: d.code
                        });
                    } else a.setData({
                        voucherNeedPurchase: !0
                    });
                }
            }
        });
    }
});